﻿Imports System.Data.SqlClient

Public Class frmRetailClientsNew

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim a As New frmSelectAccount
        a.ShowDialog()

        If SelAccNo <> "" Then
            Me.txtAccNo.Text = SelAccNo
            Me.txtAccName.Text = SelAccName
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.Close()
    End Sub

    Sub Clear()
        Me.txtClientCode.Clear()
        Me.txtProducerName.Clear()
        Me.txtTele.Clear()
        Me.txtEmail.Clear()
        Me.txtIDInfo.Clear()
        Me.txtAddress.Clear()
        Me.txtAccName.Clear()
        Me.txtAccNo.Clear()
        Me.txtMobile.Clear()
        Me.txtProducerName.Focus()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Clear()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If Me.txtProducerName.Text.Trim.Length = 0 Then
                MsgBox("Please fill in all mandatory fields")
                Exit Sub
            ElseIf Me.txtClientCode.Text.Trim.Length = 0 Then
                MsgBox("Please fill in all mandatory fields")
                Exit Sub
            ElseIf Me.txtAccName.Text.Trim.Length = 0 Then
                MsgBox("Please fill in all mandatory fields")
                Exit Sub
            ElseIf Me.txtEmail.Text.Trim.Length > 0 And _
                   Not System.Text.RegularExpressions.Regex.IsMatch(Me.txtEmail.Text.Trim, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$") Then
                MsgBox("Please enter a valid email address or leave blank")
                Exit Sub
            Else
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand("Insert Into RetailClients " & _
                                          "(Code,FullName,MobileNo,Tele,Email,IDInfo,Address,AccNo,AccName) Values " & _
                                          "(@Code,@FullName,@MobileNo,@Tele,@Email,@IDInfo,@Address,@AccNo,@AccName)", cnn)

                cnn.Open()

                'Add values
                cmd.Parameters.AddWithValue("@Code", Me.txtClientCode.Text.Trim)
                cmd.Parameters.AddWithValue("@FullName", Me.txtProducerName.Text.Trim)
                cmd.Parameters.AddWithValue("@MobileNo", Me.txtMobile.Text.Trim)
                cmd.Parameters.AddWithValue("@Tele", Me.txtTele.Text.Trim)
                cmd.Parameters.AddWithValue("@Email", Me.txtEmail.Text.Trim)
                cmd.Parameters.AddWithValue("@IDInfo", Me.txtIDInfo.Text.Trim)
                cmd.Parameters.AddWithValue("@Address", Me.txtAddress.Text.Trim)
                cmd.Parameters.AddWithValue("@AccNo", Me.txtAccNo.Text.Trim)
                cmd.Parameters.AddWithValue("@AccName", Me.txtAccName.Text.Trim)

                cmd.ExecuteNonQuery()
                cnn.Close()

                MsgBox("Saved Successfully!")

                Clear()
                Me.Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class